package com.yeh.web.controllers;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yeh.common.util.JsonUtil;
import com.yeh.model.vo.JsonVO;
import com.yeh.model.vo.User;
import com.yeh.service.MenuService;
import com.yeh.service.UserService;



/**
 * 
 * <p>Title: UserController</p>
 * <p>Description: 用户登录</p>
 * <p>Company:  Co.</p>
 * <p>Package: com.yeh.web.controllers</p>
 * @author <a href="mailto:yehshuai@126.com">叶帅</a>  
 * <p>DateTime: 2015年8月27日 ★  下午4:48:20</p> 
 * @Version V1.0.0
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
	
	private static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	@Autowired
	private MenuService menuService;
	
	@ResponseBody
	@RequestMapping(value="/login",produces="text/text;charset=UTF-8")
	public String login(User user, HttpSession session, HttpServletRequest request){
		Map<String,Object> userMap = new HashMap<String,Object>();
		userMap.put("userName", user.getUserName().toString());
		userMap.put("passWord", user.getPassWord().toString());
		JsonVO json = new JsonVO();
		if(userService.isExist(userMap)){
			logger.info("登录成功");
			json.setSuccess(true);
			json.setMsg("登陆成功！");
			return JsonUtil.object2json(json);
		}else{
			json.setMsg("用户名或密码错误！");
			return JsonUtil.object2json(json);
		}
	}
	@RequestMapping("/main")
	public String main(HttpServletRequest request,HttpServletResponse response){
		logger.debug("进入主页面");
		logger.debug(menuService.getTree().toString());
		logger.debug(menuService.getList().toString());
		return "pages/main/main";
	}
	
	@RequestMapping("/tables")
	public String tables(HttpServletRequest request,HttpServletResponse response){
		return "pages/oldlogin";
	}

}
